package com.rookie.im.core.server.handler;

import org.redisson.api.RMap;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.rookie.im.core.codec.pack.LoginPack;
import com.rookie.im.core.codec.proto.Message;
import com.rookie.im.utils.enums.Command;
import com.rookie.im.utils.Constans.ChannelAttributeKey;
import com.rookie.im.core.server.domain.LoginSession;
import com.rookie.im.core.server.utils.RabbitManager;
import com.rookie.im.core.server.utils.RedisManager;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ServerHandler extends SimpleChannelInboundHandler<Message> {
	
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception {
		log.info("{}", msg);
		
		int command = msg.getMessageHead().getCommand();
		if (command == Command.LOGIN.getCommand()) {
            LoginPack loginPack = JSONObject.parseObject(JSONObject.toJSONString(msg.getMessagePack().getData()), new TypeReference<LoginPack>() {}.getType());
            int appId = msg.getMessageHead().getAppId();
            int clientType = msg.getMessageHead().getClientType();
            String userId = loginPack.getUserId();
            String imei = msg.getMessageHead().getImei();
            
            ctx.channel().attr(AttributeKey.valueOf(ChannelAttributeKey.CHANNEL_APPID)).set(appId);
            ctx.channel().attr(AttributeKey.valueOf(ChannelAttributeKey.CHANNEL_CLIENT_TYPE)).set(clientType);
            ctx.channel().attr(AttributeKey.valueOf(ChannelAttributeKey.CHANNEL_USERID)).set(userId);
            ctx.channel().attr(AttributeKey.valueOf(ChannelAttributeKey.CHANNEL_IMEI)).set(imei);
            LoginSession loginSession = new LoginSession(appId, clientType, userId, imei, 1, null);
            
            String path = appId+":userSession:"+userId;
			RMap<String,String> map = RedisManager.getRedissonClient().getMap(path);
            String key = clientType + ":" + imei;
			map.put(key, JSONObject.toJSONString(loginSession));
            log.info("LOGIN: {}", key);
		} else if (command == Command.LOGOUT.getCommand()) {
			int appId = (int) ctx.channel().attr(AttributeKey.valueOf(ChannelAttributeKey.CHANNEL_APPID)).get();
			int clientType = (int) ctx.channel().attr(AttributeKey.valueOf(ChannelAttributeKey.CHANNEL_CLIENT_TYPE)).get();
			String userId = (String) ctx.channel().attr(AttributeKey.valueOf(ChannelAttributeKey.CHANNEL_USERID)).get();
            String imei = (String) ctx.channel().attr(AttributeKey.valueOf(ChannelAttributeKey.CHANNEL_IMEI)).get();

            String path = appId+":userSession:"+userId;
            RMap<String,String> map = RedisManager.getRedissonClient().getMap(path);
            String key = clientType + ":" + imei;
            String loginInfo = map.get(key);
            map.remove(key);
            log.info("LOGOUT: {}, {}", key, loginInfo);
            
		} else {
			RabbitManager.publish(msg);
		}
	}
}
